package com.ustarlink.live;

import android.content.Context;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cfm on 15-12-23.
 */
public class ChannelXmlPullParser {

    static final String KEY_Channel = "CHANNEL";
    private static String myTag = "uStarLink Live";
    public static List<Channel> getChannelFromFile(Context context){


        /**定义返回的 Channel 列表**/
        List<Channel> channels;
        channels = new ArrayList<Channel>();

        /** 在解析时Channel临时变量 **/
        Channel curChannel = null;

        /** 在解析时text临时变量 **/
        String curText = "";
        try {
            /** 获得factory和PullParser **/
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser  xpp = factory.newPullParser();

            /** 打开InputStream和读取XML文件 **/
            FileInputStream fis = context.openFileInput("tvs_epg.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            StringBuilder sb = new StringBuilder();

            /**************************将输出文件转为字符打印**********************************/

//            String line = null;
//            try {
//                while ((line = reader.readLine()) != null) {
//                    sb.append(line + "\n");
//                }
//            }catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                try {
//                    fis.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            Log.i("FileInputStream", sb.toString());
//            /*****************************************************************/



            /** 指向我们需要解析的文件 **/
            xpp.setInput(reader);
            Log.d(myTag, "真在解析");
            /** 获取初始化的eventType() **/

            /**循环直到END_DOCUMENT**/
            int counter = 0;
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT){

               // Log.d(myTag, "Tagname = " + eventType);
                if (xpp.getEventType() ==XmlPullParser.START_TAG){
                    /**获取当前节点**/
                    String tagname = xpp.getName();
                    if (tagname.equals(KEY_Channel)){
                        counter ++;
                        // 如果是channel开始时new Channel
                        curChannel = new Channel();
                        curChannel.setId(xpp.getAttributeValue(0));
                        curChannel.setChannelName(xpp.getAttributeValue(2));
                        curChannel.setChannelUrl(xpp.getAttributeValue(3));
                        Log.i(myTag, String.valueOf(counter) + "\t" + curChannel.toString());
                        channels.add(curChannel);
                    }
                }

                //下一次
                xpp.next();

            }

//            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT){
//                if (xpp.getEventType() == XmlPullParser.START_TAG){
//                    String name = xpp.getName();
//                    Log.i(myTag, name);
//                    if (name.equals("CHANNEL")){
//                        Channel tmp=new Channel(xpp.getAttributeValue(0), xpp.getAttributeValue(2), xpp.getAttributeName(3));
//                        channels.add(tmp);
//                        Log.i(myTag, tmp.toString());
//                    } else if (xpp.getEventType() == XmlPullParser.END_TAG){
//                    } else if (xpp.getEventType() == XmlPullParser.TEXT){
//                    }
//                    xpp.next();
//                }
//            }


        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
//        catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return channels;

    }
}
